package netty.demo02;

import io.netty.channel.nio.NioEventLoopGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * TestEventLoop
 *
 * @author chenxin
 * @date 2022/05/26 18:13
 **/
public class TestEventLoop {

    private static final Logger log = LoggerFactory.getLogger(TestEventLoop.class);

    public static void main(String[] args) {

        // 1. 创建事件循环组 io 事件， 普通任务， 定时任务
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup(2);

        // 2. 获取下一下事件循环对象
        System.out.println(eventExecutors.next());
        System.out.println(eventExecutors.next());
        System.out.println(eventExecutors.next());
        System.out.println(eventExecutors.next());

        // 3. 普通任务
        eventExecutors.next().execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            log.info("ok");
        });

        // 4. 定时任务 0: 初始执行事件 1：间隔时间
        eventExecutors.next().scheduleAtFixedRate(() -> {
            log.info("定时任务。。。。");
        }, 0, 1, TimeUnit.SECONDS);

        log.info("main");
    }

}